package top.yuyufeng.mbuy.dao;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import top.yuyufeng.mbuy.dto.PageQuery;
import top.yuyufeng.mbuy.dto.ProductDTO;
import top.yuyufeng.mbuy.entity.Product;
import top.yuyufeng.mbuy.enums.ProductStatusEnum;
import top.yuyufeng.mbuy.mapper.ProductMapper;
import top.yuyufeng.mbuy.utils.PageInfoUtil;

import java.util.List;

/**
 * @author yuyufeng7
 */
@Repository
public class ProductDao {
    @Autowired
    private ProductMapper productMapper;


    public void insert(Product product) {
        productMapper.insertSelective(product);
    }

    public PageInfo<ProductDTO> page(PageQuery pageQuery) {
        Product product = new Product();
        product.setStatus(ProductStatusEnum.ON_SALE.getKey());
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        List<Product> list = productMapper.select(product);
        PageInfo pageInfo = new PageInfo(list);
        PageInfo<ProductDTO> productDTOPageInfo = PageInfoUtil.pageInfo2PageInfoDTO(pageInfo, ProductDTO.class);
        return productDTOPageInfo;
    }


    public Product get(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }
}
